//
//  DataTimeShowMarkerView.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/30.
//

import UIKit
import Charts

public class DataTimeShowMarkerView: MarkerView {

    public var timeDatas : [String] = []
    
    public var textBgColor : UIColor = .clear {
        didSet{
            
            self.backgroundColor = textBgColor
            iconImageView.image = UIImage(named: "ic_mark_hr")?.setupImageTintColor(tintColor: textBgColor, blendModels: [.destinationIn])
        }
    }
    
    
    public var textColor: UIColor? {
        didSet{
            
            dateLabel.textColor = textColor
        }
    }
    
    public var textFont : UIFont? {
        didSet{
            dateLabel.font = textFont
        }
    }
    
    public var maxValue : CGFloat = 0
    
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        makeUI()
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeUI() {
        
        self.layer.cornerRadius = 5
        
        self.addSubview(iconImageView)
        self.addSubview(dateLabel)
        
        textColor = UIColor.init(hex: "#FFFFFF")
        textFont = Fonts.systemFont(ofSize: 10)
        
        dateLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-5)
            make.left.equalToSuperview().offset(5)
            make.top.equalToSuperview().offset(5)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom)
            make.centerX.equalToSuperview()
            
        }
        
    }
    
  
    public override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
        
        let index = Int(highlight.x)
        if timeDatas.isEmpty || index >= timeDatas.count {
            self.isHidden = true
            return
        }
        let dateStr = timeDatas[index]
        if dateStr.isEmpty {
            self.isHidden = true
            return
        }
        
        self.isHidden = false
    
        dateLabel.text = dateStr
        
//        // 更新布局
//        dateLabel.snp.updateConstraints { make in
//            make.right.equalToSuperview().offset(-5)
//            make.left.equalToSuperview().offset(5)
//
//        }
//
        
        /// 注意 代码顺序不能乱 layoutIfNeeded 必须写在 dateLabel.text 之后
        /// 好计算出高度
       
        iconImageView.layoutIfNeeded()
        dateLabel.layoutIfNeeded()
        self.layoutIfNeeded()
        
        /// 拿到高度才好设置 offset
        if let chartView = self.chartView {
            
            let drawX = highlight.drawX
         
            if drawX <= self.frame.size.width/2.0 + 5 {
                self.offset.x = 0
                
                iconImageView.left = 0
                
            } else if drawX > self.frame.size.width/2.0 && drawX < chartView.frame.size.width - self.frame.size.width/2.0 - 5 {
                
                self.offset.x = -self.frame.size.width / 2.0
                iconImageView.centerX = self.frame.size.width/2.0
            } else {
                
                self.offset.x = -self.frame.size.width + 5
                iconImageView.right = self.frame.size.width
            }
  
        }
        
        self.offset.y = -self.frame.size.height - 5.0
        
       
    }
    
    
    private lazy var iconImageView: ImageView = {
        let iconImageView = ImageView()
        iconImageView.image = UIImage(named: "ic_mark_hr")
        return iconImageView
    }()
    
    private lazy var dateLabel: Label = {
        let dateLabel = Label()
        dateLabel.textAlignment = .center
        dateLabel.numberOfLines = 0
       
        return dateLabel
    }()

}
